var index = 0;
var li=[];
Page({
  data:{
    list:li,
    name:'',
    region:'',
    door:'',
    tel:'',
    index:'',
    openid:'',
},
onShow:function () {
  var that = this;
  wx.request({
    url: 'http://82.157.94.115:2005/address1',
    method: 'GET',
    header: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    data: {

    },
    success:function(res){
      console.log('成功')
      console.log(JSON.stringify(res.data))
      that.setData({
        list:res.data.addressObj,
      })
    }
  })

},
addAddre:function(e){
  	wx.navigateTo({
       url: '../newAddre/newAddre'
    })
  },
toModifyAddre:function(e){
  console.log("选中的电话"+e.currentTarget.dataset.tel);
  console.log("选中的index"+e.currentTarget.dataset.index);
  console.log("选中的addre"+e.currentTarget.dataset.region+e.currentTarget.dataset.door);
  console.log("选中的region"+e.currentTarget.dataset.region);
  console.log("选中的openid"+e.currentTarget.dataset.openid);
  wx.navigateTo({
    url: '../modifyAddre/modifyAddre?name='+ e.currentTarget.dataset.name+"&tel="+e.currentTarget.dataset.tel+"&addre="+e.currentTarget.dataset.region+e.currentTarget.dataset.door+"&region="+ e.currentTarget.dataset.region+"&door="+e.currentTarget.dataset.door+"&index="+e.currentTarget.dataset.index  
  })
  },
  toCleanOrder:function(e){
    for(var i = 0;i<this.data.list.length;i++){
      if(i==e.currentTarget.dataset.index&&li[e.currentTarget.dataset.index].isChecked=='false'){
        li[e.currentTarget.dataset.index].checked= "true"
         console.log('选中')
         li[e.currentTarget.dataset.index].isChecked='true'
         console.log(li[e.currentTarget.dataset.index].checked)
         console.log(li[e.currentTarget.dataset.index].isChecked)
      }else if(i==e.currentTarget.dataset.index&&li[e.currentTarget.dataset.index].isChecked=='true'){
        li[e.currentTarget.dataset.index].checked= ""

         console.log('取消选中')
         li[e.currentTarget.dataset.index].isChecked='false'
         console.log(li[e.currentTarget.dataset.index].checked)
         console.log(li[e.currentTarget.dataset.index].isChecked)
      }else{
        li[i].checked= ""
        console.log('未选中')
      }
  }

},

  onLoad: function(options) {
    var flag=false;//判断是从哪个页面跳转过来
    var sign = 0//判断从修改页面中的保存还是删除按钮过来，保存为1，删除为2
  	flag =options.flag;
    sign = options.sign;
    if (flag) {
    	 li.push({
      "index":index++,
    	"name":options.name,
      "tel":options.tel,
      "region":options.region,
    	"addre":options.addre,
      "area":options.area,
      "checked":"",
      "isChecked":"false",
      "addrevalue":options.addrevalue,
      "areavalue":options.areavalue,
      "door":options.door,
    	})
        this.setData({
          list:li,
          index:options.index,
          name:options.name,
          tel:options.tel,
          region:options.region,
          door:options.door,
        })
    };
    if(sign=='1'){
      console.log("我是从修改页面过来的")
      li[options.index].region=options.region;
      li[options.index].name=options.name;
      li[options.index].tel=options.tel;
      li[options.index].addre=options.addre;
      li[options.index].area=options.area;
      li[options.index].door=options.door;
      this.setData({
          list:li,
          index:options.index,
          name:options.name,
          tel:options.tel,
          region:options.region,
          door:options.door,
        })
      console.log(options.index)

    };
    if(sign=='2'){
        li.splice(options.index, 1);
        this.setData({
          list:li,
          index:options.index,
          name:options.name,
          tel:options.tel,
          region:options.region,
          door:options.door,
        })
      };
      // wx.setStorageSync({
      //   key:'list',
      //   list:this.data.list
      // })
      // wx.request({
      //   url: 'http://82.157.94.115:2005/address',
      //   method: 'POST',
      //   header: {
      //     "Content-Type": "application/x-www-form-urlencoded"
      //   },
      //   data: {
      //     name:this.data.name,
      //     tel:this.data.tel,
      //     region:this.data.region,
      //     door:this.data.door,
      //   },
      //   success:function(){
      //     console.log('成功')
      //   }
      // })
}
})
